package br.ufal.ic.sd.serverSide.servicos;

import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class ServicoLerEntradaJMeter implements IService {

	public void executarServico(Socket clientSocket, int porta) {
		try {
			// tratando dados recebidos pelo jmeter;
			InputStream in = clientSocket.getInputStream();

			byte[] streamArray = new byte[1000];
			int n = in.read(streamArray);
			byte[] streamRequisicao = new byte[n];

			for (int i = 0; i < n; ++i) {
				streamRequisicao[i] = streamArray[i];
			}

			String requisicaoDoCliente = new String(streamRequisicao);
			System.out.println("Mensagem recebida no server: "
					+ requisicaoDoCliente);

			// saida de dados para o cliente
			OutputStreamWriter osToClient = new OutputStreamWriter(clientSocket.getOutputStream());
			BufferedWriter bwToClient = new BufferedWriter(osToClient);
			bwToClient.write(requisicaoDoCliente.toUpperCase());
			bwToClient.newLine();
			bwToClient.flush();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
